package ua.exchangeratesukraine.util;

import ua.exchangeratesukraine.ui.fragment.SetDataListener;
import ua.exchangeratesukraine.R;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

public class DataTask extends AsyncTask<String, Void, Object> {

	private Context			context		= null;

	DataTask() {
	}

	public DataTask(Context ctx, SetDataListener listener) {

		context = ctx;
		setDataListener = listener;
	}

	private SetDataListener	setDataListener;

	@Override
	protected void onPreExecute() {
		super.onPreExecute();

		setDataListener.setProgressVisible();
	}

	@Override
	protected void onPostExecute(Object result) {
		super.onPostExecute(result);

		setDataListener.setProgressInVisible();
		setDataListener.setData(result, null);
	}

	@Override
	protected Object doInBackground(String... urls) {

		Object object = new NetRequest().getResponseString(context, urls[0]);
//		Log.d("DataTask", "response = " + object.toString());
		String connectionError = context.getString(R.string.text_toast_connection_error);
		String urlConnectionError = context.getString(R.string.text_toast_url_connection_error);
		String requests_limit = "requests_limit";
		if ((object.toString()).equals(connectionError)) {

			return connectionError;
		} else if ((object.toString()).equals(urlConnectionError)) {
				
			return urlConnectionError;
		} else if ((object.toString()).contains("requests_limit")) {
			
			return requests_limit;
		} else {

			return XmlParser.parsingXml(new NetRequest().getResponseString(context, urls[0]));
		}
	}
}